package com.cheems.cheems_spring_ai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cheems.cheems_spring_ai.common.BaseResponse;
import com.cheems.cheems_spring_ai.common.ResultUtils;
import com.cheems.cheems_spring_ai.domain.AiOperation;
import com.cheems.cheems_spring_ai.service.AiOperationService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/aiOperation")
public class AIOperationController {
    @Resource
    private AiOperationService aiOperationService;
    
    /**
     * 获取全部AI操作记录（不分页）
     * @return 全部记录列表
     */
    @RequestMapping("/list")
    public BaseResponse<List<AiOperation>> list(){
        List<AiOperation> list = aiOperationService.list();
        return ResultUtils.success(list);
    }
    
    /**
     * 分页获取AI操作记录
     * @param current 当前页码
     * @param pageSize 每页大小
     * @param henauNumber 可选的学号查询参数
     * @param userName 可选的用户名查询参数
     * @return 分页结果
     */
    @GetMapping("/page")
    public BaseResponse<Page<AiOperation>> page(
            @RequestParam(value = "current", defaultValue = "1") Long current,
            @RequestParam(value = "pageSize", defaultValue = "10") Long pageSize,
            @RequestParam(value = "henauNumber", required = false) String henauNumber,
            @RequestParam(value = "userName", required = false) String userName) {
        
        // 创建分页对象
        Page<AiOperation> page = new Page<>(current, pageSize);
        
        // 构建查询条件
        LambdaQueryWrapper<AiOperation> queryWrapper = new LambdaQueryWrapper<>();
        
        // 添加可选的过滤条件
        if (henauNumber != null && !henauNumber.isEmpty()) {
            queryWrapper.eq(AiOperation::getHenauNumber, henauNumber);
        }
        
        if (userName != null && !userName.isEmpty()) {
            queryWrapper.like(AiOperation::getUserName, userName);
        }
        
        // 按创建时间降序排序
        queryWrapper.orderByDesc(AiOperation::getCreateTime);
        
        // 执行分页查询
        Page<AiOperation> result = aiOperationService.page(page, queryWrapper);
        
        return ResultUtils.success(result);
    }
}
